﻿using System;
using System.Collections.Generic;
using System.Linq;
using BBCode.Nodes;

namespace BBCode.Tags.BBCodeTags
{
    /// <summary>
    /// The Horizontal ruler tag.
    /// </summary>
    public class HorizontalRuler : BBCode, ITag
    {
        private readonly string _openingTag;

        /// <summary>
        /// Initializes a new instance of the <see cref="HorizontalRuler"/> class.
        /// </summary>
        /// <param name="name">The name of the tag.</param>
        /// <param name="templateName">Name of the template.</param>
        public HorizontalRuler(string name, string templateName)
            : base(name, templateName, true, 0)
        {
            _openingTag = "[hr]";
        }

        #region ITag Members

        /// <summary>
        /// Gets the nodes.
        /// </summary>
        /// <param name="innertext">The innertext.</param>
        /// <returns></returns>
        public new IEnumerable<INode> GetNodes(string innertext)
        {
            var index = innertext.IndexOf(_openingTag, StringComparison.OrdinalIgnoreCase);
            var openings = new List<int>();
            while (index != -1)
            {
                openings.Add(index);
                index = innertext.IndexOf(_openingTag, index + 1, StringComparison.OrdinalIgnoreCase);
            }

            return openings.Select(t => new NodeIndex {Beginning = t, End = t + 4}).Select(nodeIndex => new Nodes.BBCode(this, _openingTag, nodeIndex, _openingTag, string.Empty)).ToList();
        }

        #endregion
    }
}